{% extends "admin/change_form.html" %}

{% block extrahead %}
{{ block.super }}

<script type="text/javascript" src="{{ MEDIA_URL }}js/jquery-1.5.1.min.js"></script>
<script>
$(document).ready(function() {
  var populate_groups = function() {
    var subject = $('select#id_subject');
    var year = $('select#id_year');
    if (subject.val() != '' && year.val() != '') {
      groups.data('options').each(function(i, option) {
        var option = $(option);
        if (option.val() == 'new' || option.val().indexOf(subject.val()) != -1 && option.val().indexOf(year.val()) != -1)
          groups.append(option);
      });
    } else {
      groups.append('<option>Izberite predmet in šolsko leto</option>');
    }
  };
  
  var groups = $('select#id_group');
  groups.data('options', groups.find('option'));
  groups.find('option').remove();
  populate_groups();

  
  var changeCb = function(e) {
    var subject = $('select#id_subject');
    var year = $('select#id_year');
    var groups = $('select#id_group');
    groups.find('option').remove();
    
    e.data.cb();
  };
  $('select#id_subject').change({cb: populate_groups}, changeCb);
  $('select#id_year').change({cb: populate_groups}, changeCb);
});
</script>
{% endblock %}
